package com.xzkj.login

import android.app.Activity
import android.content.Context
import android.text.TextUtils
import com.orhanobut.MyLog
import com.umeng.socialize.PlatformConfig
import com.umeng.socialize.UMAuthListener
import com.umeng.socialize.UMShareAPI
import com.umeng.socialize.bean.SHARE_MEDIA
import okhttp3.*
import org.json.JSONObject
import java.io.IOException

object LoginManager {

    fun init(appId:String, appSecret:String){
        PlatformConfig.setWeixin(appId, appSecret)
    }

    fun login(activity: Activity, loginCallback: LoginCallback){
        UMShareAPI.get(activity).getPlatformInfo(activity, SHARE_MEDIA.WEIXIN, object : UMAuthListener {
            override fun onStart(p0: SHARE_MEDIA?) {
                MyLog.d("wxLogin onStart")
            }

            override fun onComplete(p0: SHARE_MEDIA?, p1: Int, p2: MutableMap<String, String>?) {
                MyLog.d("wxLogin onComplete: $p2")
                loginAPI(p2, loginCallback)
            }

            override fun onError(p0: SHARE_MEDIA?, p1: Int, p2: Throwable?) {
                MyLog.e("wxLogin onError: ${p2?.message}")
                loginCallback.onFailed(p1, p2?.message)
            }

            override fun onCancel(p0: SHARE_MEDIA?, p1: Int) {
                MyLog.e("wxLogin onCancel")
                loginCallback.onFailed(-3, "login cancel")
            }

        })
    }

    fun loginAPI(info: MutableMap<String, String>?, loginCallback: LoginCallback){
        val openid = info?.get("openid")
        if (TextUtils.isEmpty(openid)){
            loginCallback.onFailed(-2, "openid is null")
            return
        }
        val client = OkHttpClient()
        val body = FormBody.Builder()
            .add("open_id", openid!!)
            .build()

        val request: Request = Request.Builder()
            .url("https://www.starkj.top/appmodel/jy-user-bean/userRegisterAndLogin")
            .post(body)
            .build()

        client.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                MyLog.e("loginAPI error: ${e.message}")
                loginCallback.onFailed(-1, e.message)
            }

            override fun onResponse(call: Call, response: Response) {
                MyLog.d("loginAPI success")
                response.body?.let {
                    val jsonObject = JSONObject(String(it.bytes()))
                    val wxUserInfo = WXUserInfo()
                    wxUserInfo.openId = info["openid"]
                    wxUserInfo.unionId = info["unionid"]
                    wxUserInfo.avatar = info["iconurl"]
                    wxUserInfo.name = info["name"]
                    val data = jsonObject.optJSONObject("data")
                    if (data != null){
                        wxUserInfo.loginState = data.optInt("loginState")
                        wxUserInfo.userLevel = data.optInt("userLevel")
                    }
                    loginCallback.onSuccess(wxUserInfo)
                }

            }
        })
    }
}